﻿using System;
using System.Linq;
using K9Nano.IM.Areas.Chat.Services;
using Microsoft.Extensions.DependencyInjection;
using Polly;
using WalkingTec.Mvvm.Core;

namespace K9Nano.IM.Areas.Chat
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddChat(this IServiceCollection services)
        {
            services.AddHttpClient<AppHttpClient>()
                .AddTransientHttpErrorPolicy(p =>
                    p.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(i * 2 - 1)));

            services.AddScoped<IDataContext>(sp =>
            {
                var configs = sp.GetService<Configs>();
                return configs.ConnectionStrings.First().CreateDC();
            });

            return services;
        }
    }
}